package cn.com.surker.system.controller;

import cn.com.surker.base.system.entity.SysStorage;
import cn.com.surker.base.system.entity.SysStorageLine;
import cn.com.surker.base.system.service.SysStorageService;
import cn.com.surker.base.system.vo.SysStorageLineVo;
import cn.com.surker.common.AbstractController;
import cn.com.surker.dto.system.SysStorageDTO;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.util.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 仓位信息控制层
 */
@RestController
@RequestMapping("/system/storage")
public class SysStorageController extends AbstractController {

    @Resource
    private SysStorageService sysStorageService;


    @PostMapping(value = "/page")
    @ApiOperation(value = "分页方法", tags = "仓位信息")
    public RestResultDto pageData(@RequestBody SysStorageDTO dto){
        if (StringUtils.isNull(dto.getStatus()))
            dto.setStatus(0);
        return RestResultDto.success(sysStorageService.page(dto));
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "新增方法", tags = "仓位信息")
    public RestResultDto save(@RequestBody SysStorageDTO dto){
        CacheUser user = getUser();
        return RestResultDto.success(sysStorageService.add(dto,user));
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新编辑方法", tags = "仓位信息")
    public RestResultDto update(@RequestBody SysStorageDTO dto){
        CacheUser user = getUser();
        return sysStorageService.edit(dto,user);
    }

    @PostMapping(value = "/updatestatus")
    @ApiOperation(value = "更新状态方法", tags = "仓位信息")
    public RestResultDto updatestatus(@RequestBody SysStorageDTO dto){
        CacheUser user = getUser();
        return RestResultDto.success(sysStorageService.editStatus(dto.getId(),dto.getStatus(),user));
    }

    /**
     * 根据仓位获取仓位关联信息
     * @param storageId
     * @return
     */
    @RequestMapping(value = "/find-storage", method = RequestMethod.POST)
    public RestResultDto findStorage(Long storageId) {
        try {
            List<SysStorageLineVo> list = sysStorageService.findStorageId(storageId);
            if (list == null)
                return RestResultDto.fail("未获取到仓位关联信息数据");
            return RestResultDto.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }
    /**
     * 根据仓位信息
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/find-org", method = RequestMethod.POST)
    public RestResultDto findOrg(Long orgId, Integer type, Long materialId) {
        try {
            List<SysStorage> list = sysStorageService.findByOrg(orgId, type, materialId);
            if (list == null)
                return RestResultDto.fail("未获取到仓位关联信息数据");
            return RestResultDto.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    /**
     * 保存仓位关联信息
     * @return
     */
    @RequestMapping(value = "/save-storage-line", method = RequestMethod.POST)
    @ResponseBody
    public RestResultDto saveStorageLine(Long orgId, Long storageId,Long lineId, Integer status) {
        try {
            CacheUser user = getUser();
            if (StringUtils.isNull(orgId))
                orgId = user.getOriginId();
            SysStorageLine storageLine = new SysStorageLine(orgId, lineId, storageId);
            storageLine.setStatus(status);
            storageLine.setCreator(user.getName());

            return sysStorageService.saveStorageLine(storageLine);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

    /**
     * 更新仓位关联信息
     * @return
     */
    @RequestMapping(value = "/update-storage-line", method = RequestMethod.POST)
    @ResponseBody
    public RestResultDto updateStorageLine(Long id, Integer status ) {
        try {
            CacheUser user = getUser();
            SysStorageLine storageLine = new SysStorageLine();
            storageLine.setId(id);
            storageLine.setStatus(status);
            storageLine.setUpdator(user.getName());

            return sysStorageService.updateStorageLine(storageLine);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail(e.getMessage());
        }
    }

}
